﻿package mortal.game.view.guild.tabpanel
{
    import Message.Game.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guild.cellrender.*;
    import mortal.game.view.guild.otherpanel.*;
    import mortal.mvc.core.*;

    public class GuildMembersPanel extends GuildBasePanel
    {
        private var _listType:int = 0;
        private var _btnSortByPosition:SortButton;
        private var _btnSortByLevel:SortButton;
        private var _btnSortByFightpower:SortButton;
        private var _btnSortByTodayContribution:SortButton;
        private var _btnSortByWeekContribution:SortButton;
        private var _btnSortByTotalContribution:SortButton;
        private var _btnSortByActive:SortButton;
        private var _btnSortByOnLine:SortButton;
        private var _bmpWarnNumBg:GBitmap;
        private var _txtWarnMemberNum:GTextFiled;
        private var _txtWarnMember:GTextFiled;
        private var _pageSelecter:PageSelecter;
        private var _txtMembersNum:GTextFiled;
        private var _btnSetCondition:GButton;
        private var _btnRecruit:GButton;
        private var _btnExitGuild:GButton;
        private var _btnDisbandGuild:GButton;
        private var _btnPositonOprate:GButton;
        private var _btnKickOutMember:GButton;
        private var _outgoingBtn:GButton;
        private var _memberList:GTileList;
        private var _sortFunction:Function;
        private var _txtSate:GTextFiled;
        private static const PAGE_MEMBER_COUNT:int = 12;

        public function GuildMembersPanel(param1:int = 0)
        {
            super(ResFileConst.guildOtherPanel);
            return;
        }// end function

        public function set listType(param1:int) : void
        {
            this._listType = param1;
            if (this._listType == 0)
            {
                this._btnSortByOnLine.visible = true;
                this._memberList.setStyle("cellRenderer", GuildMembersListCellRenderer);
                this.reqData();
            }
            else if (this._listType == 1)
            {
                this._btnSortByOnLine.visible = false;
                this._memberList.setStyle("cellRenderer", GuildFindingTalentCellRenderer);
                this._txtSate.text = "" + Language.getString(71330);
            }
            return;
        }// end function

        private function reqData() : void
        {
            if (this._listType == 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_memberList_get));
                if (GuildConst.hasPermissions(GuildConst.OpenWarnListPanel))
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_warnMemberList_get));
                }
            }
            return;
        }// end function

        override protected function onResLoadComplete() : void
        {
            _isLoadComplete = true;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            _isLoadComplete = true;
            UIFactory.bg(17, 69, 703, 420, this);
            UIFactory.bg(17, 69, 703, 26, this, ImagesConst.RegionTitleBg);
            UIFactory.gTextField(Language.getString(60026), 53, 73, 54, 20, this);
            UIFactory.gTextField(Language.getString(60034), 164, 73, 30, 20, this);
            UIFactory.gTextField(Language.getString(60035), 225, 73, 65, 20, this);
            UIFactory.gTextField(Language.getString(60036), 310, 73, 40, 20, this);
            UIFactory.gTextField(Language.getString(60037), 380, 73, 54, 20, this);
            UIFactory.gTextField(Language.getString(60029), 458, 73, 54, 20, this);
            UIFactory.gTextField(Language.getString(60030), 536, 73, 40, 20, this);
            UIFactory.gTextField(Language.getString(60038), 604, 73, 40, 20, this);
            this._txtSate = UIFactory.gTextField(Language.getString(60032), 674, 73, 40, 30, this);
            this._btnSortByPosition = UIFactory.sortButton(194, 76, this);
            this._btnSortByPosition.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByPosition.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByLevel = UIFactory.sortButton(285, 76, this);
            this._btnSortByLevel.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByLevel.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByFightpower = UIFactory.sortButton(350, 76, this);
            this._btnSortByFightpower.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByFightpower.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByTodayContribution = UIFactory.sortButton(434, 76, this);
            this._btnSortByTodayContribution.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByTodayContribution.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByWeekContribution = UIFactory.sortButton(512, 76, this);
            this._btnSortByWeekContribution.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByWeekContribution.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByTotalContribution = UIFactory.sortButton(576, 76, this);
            this._btnSortByTotalContribution.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByTotalContribution.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByActive = UIFactory.sortButton(644, 76, this);
            this._btnSortByActive.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByActive.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            this._btnSortByOnLine = UIFactory.sortButton(702, 76, this);
            this._btnSortByOnLine.configEventListener(SortEvent.DESCENDING, this.onSortChange);
            this._btnSortByOnLine.configEventListener(SortEvent.ASCENDING, this.onSortChange);
            var _loc_1:* = "<a href=\'event:goodMemberLink\' target = \'\'><font color=\'#00ff00\'><u>" + Language.getString(60039) + "</u></font></a>";
            this._txtWarnMember = UIFactory.gTextField(_loc_1, 44, 460, 100, 22, this, null, true);
            this._txtWarnMember.configEventListener(TextEvent.LINK, this.onTextLink);
            this._bmpWarnNumBg = UIFactory.gBitmap(ImagesConst.NumberBg, 112, 450, this);
            var _loc_2:* = GlobalStyle.textFormatBai;
            _loc_2.size = 14;
            _loc_2.align = "center";
            this._txtWarnMemberNum = UIFactory.gTextField("99", 112, 451, 24, 24, this, _loc_2);
            var _loc_4:* = GuildConst.hasPermissions(GuildConst.OpenWarnListPanel);
            this._txtWarnMemberNum.visible = GuildConst.hasPermissions(GuildConst.OpenWarnListPanel);
            var _loc_4:* = _loc_4;
            this._bmpWarnNumBg.visible = _loc_4;
            this._txtWarnMember.visible = _loc_4;
            var _loc_3:* = GlobalStyle.textFormatBai;
            _loc_3.align = "center";
            this._pageSelecter = UIFactory.pageSelecter(302, 460, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, _loc_3);
            this._pageSelecter.maxPage = 3;
            this._pageSelecter.currentPage = 1;
            this._pageSelecter.pageTextBoxSize = 45;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._txtMembersNum = UIFactory.gTextField(Language.getString(60040) + ":25/50", 463, 459, 98, 20, this);
            this._btnSetCondition = UIFactory.gButton("" + Language.getString(71331), 647, 459, 65, 22, this);
            this._btnSetCondition.visible = GuildConst.hasPermissions(GuildConst.SetApplyCondition);
            this._btnRecruit = UIFactory.gButton(Language.getString(60003), 70, 493, 62, 24, this);
            this._btnPositonOprate = UIFactory.gButton("" + Language.getString(71332), 155, 493, 62, 24, this);
            this._btnExitGuild = UIFactory.gButton(Language.getString(60004), 337, 493, 62, 24, this);
            this._btnDisbandGuild = UIFactory.gButton(Language.getString(60005), 513, 493, 62, 24, this);
            this._btnKickOutMember = UIFactory.gButton("" + Language.getString(71333), 600, 493, 62, 24, this);
            this._outgoingBtn = UIFactory.gButton("", 425, 493, 62, 24, this);
            this._btnRecruit.visible = false;
            this._btnPositonOprate.visible = false;
            this._btnDisbandGuild.visible = false;
            this._btnKickOutMember.visible = false;
            this._outgoingBtn.visible = false;
            this._memberList = UIFactory.tileList(15, 95, 704, 396, this);
            this._memberList.rowHeight = 30;
            this._memberList.columnWidth = 668;
            this._memberList.horizontalGap = 1;
            this._memberList.verticalGap = 0;
            this._sortFunction = GuildUtil.defaultMemberListSortFunc;
            return;
        }// end function

        private function onTextLink(event:TextEvent) : void
        {
            GuildWarnMembersPanel.instance.popUp();
            return;
        }// end function

        override public function update() : void
        {
            var _loc_1:SelfGuildInfo = null;
            var _loc_5:int = 0;
            var _loc_6:SGuildMember = null;
            if (this._listType == 0)
            {
                _loc_1 = Cache.instance.guild.selfGuildInfo;
            }
            else if (this._listType == 1)
            {
                _loc_1 = Cache.instance.guild.otherGuildInfoV2;
            }
            _loc_1.memberList.sort(this._sortFunction);
            this._pageSelecter.maxPage = Math.ceil(_loc_1.memberList.length / PAGE_MEMBER_COUNT);
            var _loc_2:* = (this._pageSelecter.currentPage - 1) * PAGE_MEMBER_COUNT;
            var _loc_3:* = new DataProvider();
            var _loc_4:int = 0;
            while (_loc_4 < PAGE_MEMBER_COUNT)
            {
                
                _loc_5 = _loc_2 + _loc_4;
                if (_loc_5 >= _loc_1.memberList.length)
                {
                    break;
                }
                _loc_6 = _loc_1.memberList[_loc_5];
                _loc_3.addItem(_loc_6);
                _loc_4++;
            }
            this._memberList.dataProvider = _loc_3;
            this._memberList.drawNow();
            if (this._listType == 0)
            {
                this._txtMembersNum.text = Language.getString(60040) + ":" + _loc_1.memberList.length + "/" + _loc_1.baseInfo.maxPlayerNum;
                this._txtWarnMemberNum.text = _loc_1.warnMemberNum.toString();
                this._outgoingBtn.label = GuildConst.hasPermissions(GuildConst.TransferLeader) ? (Language.getString(71334)) : ("" + Language.getString(71335));
                if (GuildConst.hasPermissions(GuildConst.OpenWarnListPanel))
                {
                    var _loc_7:* = Boolean(_loc_1.warnMemberNum > 0);
                    this._txtWarnMemberNum.visible = Boolean(_loc_1.warnMemberNum > 0);
                    this._bmpWarnNumBg.visible = _loc_7;
                }
                this._btnPositonOprate.visible = GuildConst.hasPermissions(GuildConst.ManagerMembers);
                this._btnDisbandGuild.visible = GuildConst.hasPermissions(GuildConst.DisbandGuild);
                this._btnRecruit.visible = GuildConst.hasPermissions(GuildConst.Recruit);
                this._btnKickOutMember.visible = GuildConst.hasPermissions(GuildConst.KickOutMember);
                this._outgoingBtn.visible = GuildConst.hasPermissions(GuildConst.OutgoingPositon) || GuildConst.hasPermissions(GuildConst.TransferLeader);
            }
            else if (this._listType == 1)
            {
                this._txtMembersNum.text = Language.getString(60040) + ":" + _loc_1.memberList.length;
                this._txtWarnMemberNum.text = "";
                this._bmpWarnNumBg.visible = false;
                this._btnPositonOprate.visible = false;
                this._btnDisbandGuild.visible = false;
                this._btnRecruit.visible = false;
                this._btnKickOutMember.visible = false;
                this._outgoingBtn.visible = false;
                this._btnSetCondition.visible = false;
                this._txtWarnMember.visible = false;
                this._bmpWarnNumBg.visible = false;
                this._btnExitGuild.visible = false;
            }
            return;
        }// end function

        override public function onMouseClick(event:MouseEvent) : void
        {
            var memberInfo:SGuildMember;
            var onOutgoing:Function;
            var obj:Object;
            var playerName:String;
            var posName:String;
            var event:* = event;
            onOutgoing = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_POSITION_OPRATE, obj));
                }
                return;
            }// end function
            ;
            switch(event.target)
            {
                case this._btnRecruit:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_RECRUIT, null));
                    break;
                }
                case this._btnExitGuild:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_EXIT, null));
                    break;
                }
                case this._btnDisbandGuild:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_DISBAND, null));
                    break;
                }
                case this._btnPositonOprate:
                {
                    if (this._memberList.selectedIndex == -1)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71336));
                        return;
                    }
                    memberInfo = this._memberList.dataProvider.getItemAt(this._memberList.selectedIndex) as SGuildMember;
                    GuildAppointPanel.instance.setMemberData(memberInfo);
                    GuildAppointPanel.instance.popUp();
                    break;
                }
                case this._btnKickOutMember:
                {
                    if (this._memberList.selectedIndex == -1)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71336));
                        return;
                    }
                    memberInfo = this._memberList.dataProvider.getItemAt(this._memberList.selectedIndex) as SGuildMember;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_KICK_OUT_MEMEBER, memberInfo.miniPlayer.entityId.id));
                    break;
                }
                case this._btnSetCondition:
                {
                    GuildApplyConditionPanel.instance.popUp();
                    break;
                }
                case this._outgoingBtn:
                {
                    if (this._memberList.selectedIndex == -1)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71336));
                        return;
                    }
                    obj = new Object();
                    if (GuildConst.hasPermissions(GuildConst.TransferLeader))
                    {
                        memberInfo = this._memberList.dataProvider.getItemAt(this._memberList.selectedIndex) as SGuildMember;
                        if (memberInfo.position >= EGuildPosition._EGuildDeputyLeader)
                        {
                            obj.targetID = memberInfo.miniPlayer.entityId.id;
                            obj.position = EGuildPosition._EGuildLeader;
                            playerName = memberInfo.miniPlayer.name;
                            Alert.show(Language.getStringByParam(70487, playerName), null, Alert.OK | Alert.CANCEL, null, onOutgoing);
                        }
                        else
                        {
                            MsgManager.showRollTipsMsg(Language.getString(71337));
                            return;
                        }
                    }
                    else
                    {
                        obj.targetID = Cache.instance.role.entityInfo.entityId.id;
                        obj.position = EGuildPosition._EGuildMember;
                        posName = GuildUtil.getPosName(Cache.instance.guild.selfGuildInfo.selfInfo.position);
                        Alert.show(Language.getStringByParam(70486, posName), null, Alert.OK | Alert.CANCEL, null, onOutgoing);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.update();
            return;
        }// end function

        private function onSortChange(event:SortEvent) : void
        {
            switch(event.target)
            {
                case this._btnSortByPosition:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("position", event.type);
                    break;
                }
                case this._btnSortByLevel:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("level", event.type, true);
                    break;
                }
                case this._btnSortByFightpower:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("combat", event.type, true);
                    break;
                }
                case this._btnSortByTodayContribution:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("contributionDay", event.type);
                    break;
                }
                case this._btnSortByWeekContribution:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("contributionWeek", event.type);
                    break;
                }
                case this._btnSortByTotalContribution:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("totalContribution", event.type);
                    break;
                }
                case this._btnSortByActive:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("activity", event.type);
                    break;
                }
                case this._btnSortByOnLine:
                {
                    this._sortFunction = GuildUtil.constructMemberListSortFunc("online", event.type, true);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.update();
            return;
        }// end function

    }
}
